Imports Microsoft.VisualBasic

Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.Xml
Imports System.Security.Cryptography.X509Certificates

Imports Microsoft.Web.Services3
Imports Microsoft.Web.Services3.Design
Imports Microsoft.Web.Services3.Security
Imports Microsoft.Web.Services3.Security.Tokens



Public Class SignMessage
    Inherits SendSecurityFilter


    Dim parentAssertion As ClientAssertion
    Dim filterContext As FilterCreationContext


    Dim serviceActor As String
    Dim isClient As Boolean

    Dim security1 As Security


    Public Function Sign(ByVal envelope As SoapEnvelope) As SoapEnvelope
        SecureMessage(envelope, security1)
        Return envelope
    End Function


    Public Sub New(ByVal parentassertion As ClientAssertion, ByVal filtercontext As FilterCreationContext)
        MyBase.New(parentassertion.ServiceActor, False, parentassertion.ClientActor)
        Me.parentAssertion = parentassertion
        Me.filterContext = filtercontext

    End Sub


    Public Overrides Sub SecureMessage(ByVal envelope As SoapEnvelope, ByVal security As Security)
        Dim token As X509SecurityToken = GetSecurityToken("CN=InfosysSDNE1Test")
        If token Is Nothing Then
            Throw New SecurityFault("Message Requirements could not be satisfied.")
        End If

        ' Add the security token to the WS-Security SOAP header. 
        security.Tokens.Add(token)

        ' Specify the security token to sign the message with.
        Dim sig As New MessageSignature(token)
        security.Elements.Add(sig)
    End Sub 'SecureMessage



    Public Function GetSecurityToken(ByVal subjectName As String) As X509SecurityToken
        Dim securityToken As X509SecurityToken = Nothing
        Dim store As X509Store = New X509Store(StoreName.My, _
          StoreLocation.CurrentUser)
        store.Open(OpenFlags.ReadOnly)
        Try
            Dim certs As X509Certificate2Collection = _
              store.Certificates.Find(X509FindType.FindBySubjectDistinguishedName, _
              subjectName, False)
            Dim cert As X509Certificate2
            If certs.Count = 1 Then
                cert = certs(0)
                securityToken = New X509SecurityToken(cert)
            Else
                securityToken = Nothing
            End If
        Catch ex As Exception
            Console.WriteLine(ex.ToString())
        Finally
            If Not (store Is Nothing) Then
                store.Close()
            End If
        End Try
        Return securityToken
    End Function
End Class


Public Class ClientAssertion
    Inherits SecurityPolicyAssertion

    Public Overrides Function CreateClientInputFilter(ByVal context As Microsoft.Web.Services3.Design.FilterCreationContext) As Microsoft.Web.Services3.SoapFilter
        Return New SignMessage(Me, context)
    End Function

    Public Overrides Function CreateClientOutputFilter(ByVal context As Microsoft.Web.Services3.Design.FilterCreationContext) As Microsoft.Web.Services3.SoapFilter

        Return Nothing

    End Function

    Public Overrides Function CreateServiceInputFilter(ByVal context As Microsoft.Web.Services3.Design.FilterCreationContext) As Microsoft.Web.Services3.SoapFilter

        Return Nothing
    End Function

    Public Overrides Function CreateServiceOutputFilter(ByVal context As Microsoft.Web.Services3.Design.FilterCreationContext) As Microsoft.Web.Services3.SoapFilter
        Return Nothing
    End Function
End Class